using Material.Mvc.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

namespace Material.Mvc.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }


        public IActionResult ProductionPlan()
        {
            // ��ʼ����ģ�ͣ�ʵ��������ǰ��ͨ��API��ȡ
            var model = new Product_PlanModel
            {
                ProductItems = new System.Collections.Generic.List<ProductItem>()
            };
            return View(model);
        }

        /// <summary>
        /// �����ƻ�����ҳ��
        /// </summary>
        public IActionResult ProductionPlanDetail()
        {
            // ����ҳ������ݽ���ǰ��ͨ��API��ȡ
            return View();
        }

        /// <summary>
        /// �����ƻ��б�ҳ��
        /// </summary>
        public IActionResult ProductionPlanList()
        {
            // ֱ�ӷ��ؿյķ�ҳ���ģ�ͣ����ݽ���ǰ��ͨ��API��ȡ
            var queryModel = new ProductionPlanQueryModel();
            var result = new ProductionPlanPagedResult
            {
                Items = new System.Collections.Generic.List<Production_PlanList>(),
                TotalCount = 0,
                PageIndex = 1,
                PageSize = 10
            };
            ViewBag.QueryModel = queryModel;
            return View(result);
        }

        /// <summary>
        /// 生产工单页面
        /// </summary>
        public IActionResult OrderShow()
        {
            // 返回生产工单页面，数据将通过前端通过API获取
            return View();
        }

        /// <summary>
        /// 新增生产工单页面
        /// </summary>
        public IActionResult OrderAdd()
        {
            // 返回新增生产工单页面
            return View();
        }

        /// <summary>
        /// 生产工单详情页面
        /// </summary>
        public IActionResult OrderDetail(int id = 1)
        {
            // 返回生产工单详情页面，id参数用于获取具体工单信息
            ViewBag.OrderId = id;
            return View();
        }

        /// <summary>
        /// 获取工单详情API
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetOrderDetail(int id)
        {
            try
            {
                // 调用后端API获取工单详情数据
                using var httpClient = new HttpClient();
                httpClient.Timeout = TimeSpan.FromSeconds(30); // 增加超时时间
                
                // 使用正确的API地址和参数格式
                var apiUrl = $"http://localhost:5299/api/WorkOrder/GetWorkOrderDetailsShow?id={id}";
                
                _logger.LogInformation($"正在调用API获取工单详情: {apiUrl}");
                
                var response = await httpClient.GetAsync(apiUrl);
                _logger.LogInformation($"API响应状态: {response.StatusCode}");
                
                if (response.IsSuccessStatusCode)
                {
                    var jsonContent = await response.Content.ReadAsStringAsync();
                    _logger.LogInformation($"API返回数据: {jsonContent}");
                    return Content(jsonContent, "application/json");
                }
                else
                {
                    var errorContent = await response.Content.ReadAsStringAsync();
                    _logger.LogError($"API调用失败: {response.StatusCode}, 错误内容: {errorContent}");
                    return Json(new { success = false, message = $"获取工单详情失败: {response.StatusCode}", error = errorContent });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取工单详情时发生错误: {ex.Message}");
                return Json(new { success = false, message = $"获取工单详情时发生错误: {ex.Message}" });
            }
        }



    }
}
